#include "vsend.h"
#include "ui_vsend.h"
#include "Tools/cconversion.h"
#include "Tools/csettings.h"
#include "View/vtoast.h"
#include <QNetworkInterface>
VSend::VSend(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::VSend)
{
    ui->setupUi(this);
    this->refreshIPList();

    ui->spinBox_auto_span->hide();
    m_pTimerAutoSend = new QTimer(this);
    connect(m_pTimerAutoSend,&QTimer::timeout,[=]{
        on_pushButton_send_clicked();
    });


}

VSend::~VSend()
{
    delete ui;
}

void VSend::setConnectionProxy(CUdp *pConnectManager)
{
    m_pConnect = pConnectManager;
    connect(m_pConnect,&CUdp::sigStateChanged,[=](CUdp::EnmState enmState){
        this->stateChanged(enmState);
    });
    connect(m_pConnect,&CUdp::sigConnectChanged,[=](QString strIp,quint16 nPort,bool bState){
        this->connectStateChanged(strIp,nPort,bState);
    });
    connect(m_pConnect,&CUdp::sigDataArrive,[=](QString strIp,quint16 nPort,QByteArray byteArr){
        this->dataArrive(strIp,nPort,byteArr);
    });
}
void VSend::refreshIPList()
{
    // 清除旧的IP地址列表
    ui->comboBox_ip->clear();

    // 获取所有网络接口
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();

    // 遍历每个网络接口
    foreach (QNetworkInterface interface, interfaces) {
        // 排除回环接口和虚拟接口
        if (interface.flags().testFlag(QNetworkInterface::IsLoopBack))
            continue;

        // 获取该接口上的所有地址
        QList<QNetworkAddressEntry> addresses = interface.addressEntries();
        foreach (QNetworkAddressEntry address, addresses) {
            // 判断地址是否为IPv4地址
            if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) {
                ui->comboBox_ip->addItem(address.ip().toString());
            }
        }
    }
}
void VSend::processSendText(QString &strText, bool &bHex, bool &bIsaddEnter)
{
    //字符串转二进制
    QByteArray byteArr;

    if(bHex){
        bool bIsOk;

        byteArr = CConversion::hexStr2ByteArray(strText,&bIsOk);
        if(bIsaddEnter) {
            byteArr.append("\r\n");
        }
        // qDebug()<<"send hex  data :"<<byteArr;
        if (false == bIsOk) {
            VToast::getInstance()->show(tr("十六进制字符有误"));
            // CUIHelper::widgetShake(ui->textEdit_send,5); //抖动提示
            qWarning()<<"十六进制文本输入格式有误:"<<strText;
        }
    }else{
        byteArr = CSettings::getInstance()->str2ByteArray(strText);  //根据设置的字符编码 进行String转换byteArr
        if(bIsaddEnter) {
            byteArr.append("\r\n");
        }
    }

    // qDebug()<<"send data :"<<byteArr;
    m_pConnect->send(ui->comboBox_ip->currentText(),ui->lineEdit_port->text().toUShort(),byteArr);
}

void VSend::on_pushButton_send_clicked()
{
    QString strText = ui->textEdit_send->toPlainText();

    bool bIsHex = ui->pushButton_hex->isChecked();
    bool bIsaddEnter = ui->pushButton_enter->isChecked();
    this->processSendText(strText,bIsHex,bIsaddEnter);

}


void VSend::on_checkBox_auto_send_clicked(bool checked)
{
    ui->pushButton_send->setEnabled(!checked);
    if(checked){
        ui->spinBox_auto_span->show();
        m_pTimerAutoSend->start(ui->spinBox_auto_span->value());
    }else{
        ui->spinBox_auto_span->hide();
        m_pTimerAutoSend->stop();
    }
}


void VSend::on_spinBox_auto_span_valueChanged(int arg1)
{
    if(ui->checkBox_auto_send->isChecked()) {
        m_pTimerAutoSend->start(arg1);
    }
}

void VSend::on_pushButton_clear_clicked()
{
    ui->textEdit_send->clear();
}

